package com.rabbit.controller;

import com.rabbit.annotation.Log;
import com.rabbit.annotation.LoginUser;
import com.rabbit.constant.OperType;
import com.rabbit.db.entity.User;
import com.rabbit.exception.BusinessException;
import com.rabbit.service.MenuService;
import com.rabbit.util.ResultGenUtil;
import com.rabbit.validate.base.Delete;
import com.rabbit.validate.base.Insert;
import com.rabbit.validate.base.Update;
import com.rabbit.vo.ResultVO;
import com.rabbit.vo.req.MenuReq;
import com.rabbit.vo.res.MenuRes;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <h1>菜单管理</h1>
 *
 * @author zjw
 * @date 2022/1/14 14:31
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Resource
    private MenuService menuService;

    /**
     * 查询用户菜单
     */
    @GetMapping("/queryUserMenu")
    public ResultVO<Object> queryUserMenu(@LoginUser User user) {
        List<MenuRes> menuResList = menuService.queryUserMenu(user);
        return ResultGenUtil.genSuccess(menuResList);
    }

    /**
     * 菜单列表
     */
    @GetMapping("/list")
    public ResultVO<Object> list(MenuReq menuReq) {
        return ResultGenUtil.genSuccess(menuService.queryMenuList(menuReq));
    }

    /**
     * 树形菜单
     */
    @GetMapping("/queryMenuTree")
    public ResultVO<Object> queryMenuTree(MenuReq menuReq) {
        return ResultGenUtil.genSuccess(menuService.queryMenuTree(menuReq));
    }

    /**
     * 查询菜单信息
     */
    @GetMapping("/queryMenuById")
    public ResultVO<Object> queryMenuById(@RequestParam Long menuId) {
        return ResultGenUtil.genSuccess(menuService.queryMenuById(menuId));
    }

    /**
     * 新增菜单
     */
    @PostMapping("/add")
    @Log(name = "菜单管理", type = OperType.INSERT)
    public ResultVO<Object> addMenu(@LoginUser User loginUser, @Validated(Insert.class) @RequestBody MenuReq menuReq) throws BusinessException {
        menuService.checkUnique(menuReq, 1);
        menuReq.setOperatorUserName(loginUser.getUserName());
        menuService.addMenu(menuReq);
        return ResultGenUtil.genSuccess();
    }

    /**
     * 修改菜单
     */
    @PostMapping("/update")
    @Log(name = "菜单管理", type = OperType.UPDATE)
    public ResultVO<Object> updateMenu(@LoginUser User loginUser, @Validated(Update.class) @RequestBody MenuReq menuReq) throws BusinessException {
        menuService.checkUnique(menuReq, 2);
        menuReq.setOperatorUserName(loginUser.getUserName());
        menuService.updateMenu(menuReq);
        return ResultGenUtil.genSuccess();
    }

    /**
     * 删除菜单
     */
    @PostMapping("/delete")
    @Log(name = "菜单管理", type = OperType.DELETE)
    public ResultVO<Object> deleteMenu(@Validated(Delete.class) @RequestBody MenuReq menuReq) throws BusinessException {
        menuService.checkCanBeDeleted(menuReq);
        menuService.deleteMenu(menuReq);
        return ResultGenUtil.genSuccess();
    }
}
